package com.ay.selfservicemachine.base;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.transition.Fade;
import android.transition.Transition;
import android.view.KeyEvent;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.view.WindowCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.ay.selfservicemachine.R;


@SuppressWarnings("unchecked")
public abstract class BaseActivity<B, V extends IContract.IView, P extends Presenter<V>> extends HQWAppCompatActivity {

    public static final int FADE = 1;
    public static final int SLIDE_LEFT = 2;
    public static final int SLIDE_RIGHT = 3;
    public static final int SLIDE_UP = 4;
    public static final int SLIDE_DOWN = 5;

//    protected abstract int attachResource();

    public B root;

    protected abstract B viewBinding();

    public P mPresenter;

    protected abstract P createPresenter();

    private int ANIMATION_TYPE = SLIDE_RIGHT;

    public void setViewAnimation(int type) {
        ANIMATION_TYPE = type;
    }

    protected FragmentManager fm;
    protected Fragment currentFragment;

    //return the id of FrameLayout
    protected abstract int setFragmentContainerResId();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setRequestedOrientation(getOrientation());
        super.onCreate(savedInstanceState);
        root = viewBinding();
        setContentView(((androidx.viewbinding.ViewBinding) root).getRoot());
//        setContentView(attachResource());
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
            mPresenter.getPrePageData();
            mPresenter.initModule();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            initWindowTranslation(getWindowTransition());
        }
//        ButterKnife.bind(this);
        initView();
        mPresenter.initData();
        setStartAnimation();
    }

    public void setStartAnimation() {

    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mPresenter != null) {
            mPresenter.resetView();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.refreshData();
        }
    }

    @Override
    protected void onDestroy() {
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter.releaseModule();
//            mPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    public void toNextActivity(Class<? extends BaseActivity> activityClass, Bundle bundle, int flag) {
        Intent intent = new Intent(this, activityClass);
        intent.setFlags(flag);
        intent.putExtras(bundle);
        startActivity(intent);
        if (ANIMATION_TYPE == FADE) {
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        } else if (ANIMATION_TYPE == SLIDE_LEFT) {
            overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
        } else if (ANIMATION_TYPE == SLIDE_RIGHT) {
            overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
        } else if (ANIMATION_TYPE == SLIDE_DOWN) {
            overridePendingTransition(R.anim.slide_down_in, R.anim.slide_down_out);
        } else if (ANIMATION_TYPE == SLIDE_UP) {
            overridePendingTransition(R.anim.slide_up_in, R.anim.slide_up_out);
        }

    }

    public void toNextActivity(Class<? extends BaseActivity> activityClass, Bundle bundle) {
        Intent intent = new Intent(this, activityClass);
        intent.putExtras(bundle);
        startActivity(intent);
        if (ANIMATION_TYPE == FADE) {
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        } else if (ANIMATION_TYPE == SLIDE_LEFT) {
            overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
        } else if (ANIMATION_TYPE == SLIDE_RIGHT) {
            overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
        } else if (ANIMATION_TYPE == SLIDE_DOWN) {
            overridePendingTransition(R.anim.slide_down_in, R.anim.slide_down_out);
        } else if (ANIMATION_TYPE == SLIDE_UP) {
            overridePendingTransition(R.anim.slide_up_in, R.anim.slide_up_out);
        }
    }


    public void overridePendingTransition(int ANIMATION_TYPE){
        if (ANIMATION_TYPE == FADE) {
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        } else if (ANIMATION_TYPE == SLIDE_LEFT) {
            overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
        } else if (ANIMATION_TYPE == SLIDE_RIGHT) {
            overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
        } else if (ANIMATION_TYPE == SLIDE_DOWN) {
            overridePendingTransition(R.anim.slide_down_in, R.anim.slide_down_out);
        } else if (ANIMATION_TYPE == SLIDE_UP) {
            overridePendingTransition(R.anim.slide_up_in, R.anim.slide_up_out);
        }
    }


    public Intent toNextActivityIntent(Class<? extends BaseActivity> activityClass, Bundle bundle) {
        Intent intent = new Intent(this, activityClass);
        intent.putExtras(bundle);
        return intent;
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
//        ActivityCollector.removeActivity(this);
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    private void initWindowTranslation(Transition transition) {
        getWindow().setReturnTransition(transition);
        getWindow().setExitTransition(transition);
        getWindow().setEnterTransition(transition);
        getWindow().setReenterTransition(transition);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    protected Transition getWindowTransition() {
        return new Fade();
    }

    public void showFragment(Fragment fragment, int position) {
        if (fm == null) {
            fm = getSupportFragmentManager();
        }
        FragmentTransaction transaction = fm.beginTransaction();
        //Fragment添加
        if (!fragment.isAdded()) {
//            fragment.setArguments(bundle);
            transaction.add(setFragmentContainerResId(), fragment, position + "");
        }
        if (currentFragment == null) {
            currentFragment = fragment;
        }
        //通过tag进行过渡动画滑动判断
        if (Integer.parseInt(currentFragment.getTag()) >= Integer.parseInt(fragment.getTag())) {
            transaction.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_right_out);
        } else {
            transaction.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_left_out);
        }

        transaction.hide(currentFragment).show(fragment);
        transaction.commit();
        currentFragment = fragment;
    }

    /**
     * 横竖屏，默认是竖屏
     * 1.landscape：横屏(风景照) ，显示时宽度大于高度；
     * 2.portrait：竖屏 (肖像照) ， 显示时 高 度大于 宽 度 ；
     * 3.user：用户当前的首选方向；
     * 4.behind：继承Activity堆栈中当前Activity下面的那个Activity的方向；
     * 5.sensor：由物理感应器决定显示方向，它取决于用户如何持有设备，当 设备 被旋转时方向会随之变化——在横屏与竖屏之间；
     * 6.nosensor：忽略物理感应器——即显示方向与物理感应器无关，不管用户如何旋转设备显示方向都不会随着改变("unspecified"设置除外)；
     * 7.unspecified ：未指定，此为默认值，由Android系统自己选择适当的方向，选择策略视具体设备的配置情况而定，因此不同的设备会有不同的方向选择；
     */
    protected int getOrientation() {
        return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    }

    protected void initView() {

    }



}
